﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Globalization;

public partial class Report_ReturnOrderStatistics : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("901000");
        if (!IsPostBack)
        {
            ORDER_OFCODE(); //加载订单来源
            Init_zdr();     //制单人
            ORDER_DTCODE(); //交易类型
            InitPayType();  //支付方式
            Init_Suggestion();//意见类型初始化
            InitReason();//退货原因
            Tbx_CustomerCode.Attributes.Add("onclick", "MultiSearchVIP('../');");
            txtSP.Attributes.Add("onclick", "SearchProduct_Report('../')");
            Session["FCode"] = "901000";
        }
    }
    #region 订单来源
    private void ORDER_OFCODE()
    {/*
        List<Model.ORDERFROM> properties = new BLL.ORDERFROM().GetAllList();
        DDL_OrderFrom.DataSource = properties;
        DDL_OrderFrom.DataTextField = "OF_NAME";
        DDL_OrderFrom.DataValueField = "OF_CODE";
        DDL_OrderFrom.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DDL_OrderFrom.Items.Insert(0, li);*/
    }
    #endregion

    #region 制单人/订单审核人/财务审核人/作废人/结算人初始化
    private void Init_zdr()
    {/*
        List<Model.USERS> lists = new BLL.USERS().GetList(null, "[USERS_STOP]=0", null);
        DDL_OrderCreater.DataSource = lists;
        DDL_OrderCreater.DataTextField = "Users_Name";
        DDL_OrderCreater.DataValueField = "Users_Code";
        DDL_OrderCreater.DataBind();

        //订单审核人
        DDL_Checker.DataSource = lists;
        DDL_Checker.DataTextField = "Users_Name";
        DDL_Checker.DataValueField = "Users_Code";
        DDL_Checker.DataBind();

        //财务审核人
        DDL_aChecker.DataSource = lists;
        DDL_aChecker.DataTextField = "Users_Name";
        DDL_aChecker.DataValueField = "Users_Code";
        DDL_aChecker.DataBind();


        //结算人
        DDL_Clearer.DataSource = lists;
        DDL_Clearer.DataTextField = "Users_Name";
        DDL_Clearer.DataValueField = "Users_Code";
        DDL_Clearer.DataBind();

        //作废人
        DDL_Deleter.DataSource = lists;
        DDL_Deleter.DataTextField = "Users_Name";
        DDL_Deleter.DataValueField = "Users_Code";
        DDL_Deleter.DataBind();

        ListItem li = new ListItem();
        li.Value = "-1";
        li.Text = "选择";
        DDL_OrderCreater.Items.Insert(0, li);
        DDL_Checker.Items.Insert(0, li);
        DDL_aChecker.Items.Insert(0, li);
        DDL_Deleter.Items.Insert(0, li);
        DDL_Clearer.Items.Insert(0, li);*/
    }

    #endregion

    #region 交易类型
    private void ORDER_DTCODE()
    {/*
        List<Model.DEALTYPE> properties = new BLL.DEALTYPE().GetAllList();
        DDL_tradetype.DataSource = properties;
        DDL_tradetype.DataTextField = "DT_NAME";
        DDL_tradetype.DataValueField = "DT_CODE";
        DDL_tradetype.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DDL_tradetype.Items.Insert(0, li);*/
    }
    #endregion

    #region 支付方式初始化
    private void InitPayType()
    {/*
        //绑定支付方式
        List<Model.PAYTYPE> paytype = new List<Model.PAYTYPE>();
        paytype = new BLL.PAYTYPE().GetAllList();
        DDL_PayType.DataSource = paytype;
        DDL_PayType.DataTextField = "PT_NAME";
        DDL_PayType.DataValueField = "PT_CODE";
        DDL_PayType.DataBind();
        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DDL_PayType.Items.Insert(0, li);*/
    }
    #endregion

    #region 意见类型
    private void Init_Suggestion()
    {/*
        DDL_ORDER_STATETYPECODE.DataSource = new BLL.STATETYPE().GetAllList();
        DDL_ORDER_STATETYPECODE.DataTextField = "ST_NAME";
        DDL_ORDER_STATETYPECODE.DataValueField = "ST_CODE";
        DDL_ORDER_STATETYPECODE.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DDL_ORDER_STATETYPECODE.Items.Insert(0, li);*/
    }
    #endregion
    #region 退货原因
    private void InitReason()
    {/*
        DDL_Reason.DataSource = new BLL.RETURNTYPE().GetAllList();
        DDL_Reason.DataTextField = "RT_NAME";
        DDL_Reason.DataValueField = "RT_CODE";
        DDL_Reason.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部";
        li.Value = "-1";
        DDL_Reason.Items.Insert(0, li);*/
    }
    #endregion
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Repeater rp = (Repeater)sender;
            DataTable dt = (DataTable)rp.DataSource;

            int Return_SUM = 0;//退单数量合计
            int sl_sum = 0;     //数量合计
            decimal spje_sum = 0;   //商品金额合计
            decimal wlje_sum = 0;   //物流金额合计
            decimal rlje_sum = 0;   //让利金额合计
            decimal sjje_sum = 0;   //实际金额合计
            decimal thjf_sum = 0;  //退还积分合计
            decimal jsje_sum = 0;  //结算金额合计
            foreach (DataRow row in dt.Rows)
            {
                Return_SUM += 1;
                sl_sum += int.Parse(row["RD_QTY"].ToString(), NumberStyles.Float);
                spje_sum += decimal.Parse(row["RD_AMT"].ToString());
                wlje_sum += decimal.Parse(row["RD_TRANCOST"].ToString());
                rlje_sum += decimal.Parse(row["RD_OFFERAMT"].ToString());
                sjje_sum += decimal.Parse(row["RD_AAMT"].ToString());
                thjf_sum += decimal.Parse(row["RD_SUBITG"].ToString());
                jsje_sum += decimal.Parse(row["RD_CLEAREDAMT"].ToString());
            }
            Label lbl_Return_SUM = (Label)e.Item.FindControl("lbl_Return_SUM");
            Label lbl_SL_SUM = (Label)e.Item.FindControl("lbl_SL_SUM");
            Label lbl_SPJE_SUM = (Label)e.Item.FindControl("lbl_SPJE_SUM");
            Label lbl_WL_SUM = (Label)e.Item.FindControl("lbl_WL_SUM");
            Label lbl_RL_SUM = (Label)e.Item.FindControl("lbl_RL_SUM");
            Label lbl_SJJE_SUM = (Label)e.Item.FindControl("lbl_SJJE_SUM");
            Label lbl_THJF_SUM = (Label)e.Item.FindControl("lbl_THJF_SUM");
            Label lbl_JSJE_SUM = (Label)e.Item.FindControl("lbl_JSJE_SUM");

            lbl_Return_SUM.Text = Return_SUM.ToString();
            lbl_SL_SUM.Text = sl_sum.ToString();
            lbl_SPJE_SUM.Text = string.Format("{0:F}", spje_sum);
            lbl_WL_SUM.Text = string.Format("{0:F}", wlje_sum);
            lbl_RL_SUM.Text = string.Format("{0:F}", rlje_sum);
            lbl_SJJE_SUM.Text = string.Format("{0:F}", sjje_sum);
            lbl_THJF_SUM.Text = string.Format("{0:F}", thjf_sum);
            lbl_JSJE_SUM.Text = string.Format("{0:F}", jsje_sum);
        }
    }
    protected void Btn_stac_Click(object sender, EventArgs e)
    {
        Bind(false);
    }
    protected void Btn_Export_Click(object sender, EventArgs e)
    {
        Bind(true);
    }

    #region 绑定
    private DataSet GetData()
    {
        string where = " where 1=1";
        where += Tbx_RDID.Text.Trim() == "" ? "" : " and RD_ID like'%" + Tbx_RDID.Text.Trim() + "%'";
        where += Tbx_LinkOrder.Text.Trim() == "" ? "" : " and [ORDERDOC].Order_ID='" + Tbx_LinkOrder.Text.Trim() + "'";
        where += DDL_OrderFrom.Value == "" ? "" : " and [ORDERDOC].ORDER_OFCODE IN (SELECT * FROM dbo.Split('" + DDL_OrderFrom.Value + "',','))";
        where += Tbx_TranId.Text.Trim() == "" ? "" : " and RD_TRANID='" + Tbx_TranId.Text.Trim() + "'";
        where += Tbx_CustomerCode.Text.Trim() == "" ? "" : " and RD_CUSTOMERCODE in (select * from dbo.Split('" + Tbx_CustomerCode.Text.Trim() + "',','))";

        where += Tbx_DealNo.Text.Trim() == "" ? "" : " and RDPD_DEALID='" + Tbx_DealNo.Text.Trim() + "'";
        where += DDL_SelDeal.SelectedValue == "-1" ? "" : " and RD_SECDEAL='" + DDL_SelDeal.SelectedValue + "'";
        where += Tbx_Returner.Text.Trim() == "" ? "" : " and RD_RETURNER='" + Tbx_Returner.Text.Trim() + "'";
        where += Tbx_ZH.Text.Trim() == "" ? "" : " and RDPD_ACCOUNT='" + Tbx_ZH.Text.Trim() + "'";
        where += Tbx_OrderNote.Text.Trim() == "" ? "" : " and RD_NOTE='" + Tbx_OrderNote.Text.Trim() + "'";
        where += DDL_ORDER_STATETYPECODE.Value == "" ? "" : " and ORDER_STATETYPECODE IN (SELECT * FROM dbo.Split('" + DDL_ORDER_STATETYPECODE.Value + "',','))";
        where += HttpContext.Current.Request.Form.Get("txtSP") == "" ? "" : " and RDD_CMDCODE in (select * from dbo.Split('" + HttpContext.Current.Request.Form.Get("txtSP") + "',','))";

        where += Tbx_Duty.Text.Trim() == "" ? "" : " and RD_DUTY='" + Tbx_Duty.Text.Trim() + "'";
        where += DDL_Reason.Value == "" ? "" : " and RD_REASON in (select * from dbo.Split('" + DDL_Reason.Value + "',','))";
        where += DDL_tradetype.Value == "" ? "" : " and RDPD_DTCODE IN (SELECT * FROM dbo.Split('" + DDL_tradetype.Value + "',','))";
        where += DDL_PayType.Value == "" ? "" : " and RDPD_PAYTYPE IN (SELECT * FROM dbo.Split('" + DDL_PayType.Value + "',','))";

        where += Tbx_PayNote.Text.Trim() == "" ? "" : " and RDPD_NOTE='" + Tbx_PayNote.Text.Trim() + "'";
        where += DDL_OrderCreater.Value == "" ? "" : " and RD_CREATER in (select * from dbo.Split('" + DDL_OrderCreater.Value + "',','))";

        where += DDL_Check.SelectedValue == "-1" ? "" : " and RD_Check='" + DDL_Check.SelectedValue + "'";
        where += DDL_Clear.SelectedValue == "-1" ? "" : " and RD_CLEAR='" + DDL_Clear.SelectedValue + "'";
        where += DDL_Acheck.SelectedValue == "-1" ? "" : " and RD_ACHECK='" + DDL_Acheck.SelectedValue + "'";
        where += DDL_ZF.SelectedValue == "-1" ? "" : " and RD_DELETE='" + DDL_ZF.SelectedValue + "'";

        where += DDL_Checker.Value == "" ? "" : " and RD_CHECKER in (select * from dbo.Split('" + DDL_Checker.Value + "',','))";
        where += DDL_aChecker.Value == "" ? "" : " and RD_ACHECKER in (select * from dbo.Split('" + DDL_aChecker.Value + "',','))";
        where += DDL_Clearer.Value == "" ? "" : " and RD_ACHECKER in (select * from dbo.Split('" + DDL_Clearer.Value + "',','))";
        where += DDL_Deleter.Value == "" ? "" : " and RD_DELETER in (select * from dbo.Split('" + DDL_Deleter.Value + "',','))";

        where += Tbx_OrderCreateF.Text.Trim() == "" ? "" : " and RD_CREATEDATE>='" + Tbx_OrderCreateF.Text.Trim() + "'";
        where += Tbx_OrderCreateT.Text.Trim() == "" ? "" : " and RD_CREATEDATE<='" + Tbx_OrderCreateT.Text.Trim() + "'";
        
        where += Tbx_CheckDateF.Text.Trim() == "" ? "" : " and RD_CHECKDATE>='" + Tbx_CheckDateF.Text.Trim() + "'";
        where += Tbx_CheckDateT.Text.Trim() == "" ? "" : " and RD_CHECKDATE<='" + Tbx_CheckDateT.Text.Trim() + "'";

        where += Tbx_AcheckDateF.Text.Trim() == "" ? "" : " and RD_ORDER_ACHECKDATE>='" + Tbx_AcheckDateF.Text.Trim() + "'";
        where += Tbx_AcheckDateT.Text.Trim() == "" ? "" : " and RD_ORDER_ACHECKDATE<='" + Tbx_AcheckDateT.Text.Trim() + "'";

        where += Tbx_ZFDateF.Text.Trim() == "" ? "" : " and RD_DELETEDATE>='" + Tbx_ZFDateF.Text.Trim() + "'";
        where += Tbx_ZFDateT.Text.Trim() == "" ? "" : " and RD_DELETEDATE<='" + Tbx_ZFDateT.Text.Trim() + "'";

        where += Tbx_PayDateF.Text.Trim() == "" ? "" : " and RDPD_PAYDATE>='" + Tbx_PayDateF.Text.Trim() + "'";
        where += Tbx_PayDateF.Text.Trim() == "" ? "" : " and RDPD_PAYDATE<='" + Tbx_PayDateF.Text.Trim() + "'";

        where += Tbx_DealDateF.Text.Trim() == "" ? "" : " and RDPD_DEALDATE>='" + Tbx_DealDateF.Text.Trim() + "'";
        where += Tbx_DealDateF.Text.Trim() == "" ? "" : " and RDPD_DEALDATE<='" + Tbx_DealDateF.Text.Trim() + "'";
        
        
        DataSet ds = new BLL.ORDERDOC().GetReturnOrderStatistics(where);
        return ds;
    }
    #endregion
    private void Bind(bool isExport)
    {
        DataSet ds = GetData();
        if (ds != null)
        {
            if (!isExport)
            {
                Repeater1.DataSource = ds.Tables[0];
                Repeater1.DataBind();
            }
            else
            { DataTable2Excel(ds.Tables[0]); }
        }
        else
        {
            Share.Alert(Page, "无统计数据");
        }
    }
    /// <summary>
    /// DataTable导出Excel
    /// </summary>
    /// <param name="dtData"></param>
    public static void DataTable2Excel(System.Data.DataTable dtData)
    {
        System.Web.UI.WebControls.DataGrid dgExport = null;
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;
        System.IO.StringWriter strWriter = null;
        System.Web.UI.HtmlTextWriter htmlWriter = null;

        if (dtData != null)
        {
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";

            strWriter = new System.IO.StringWriter();
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

            dgExport = new System.Web.UI.WebControls.DataGrid();
            
            dtData.Columns.Add("含担保交易");
            dtData.Columns.Add("审核");
            dtData.Columns.Add("财务审核");
            dtData.Columns.Add("结算");
            dtData.Columns.Add("作废");

            dtData.Columns["RD_ID"].ColumnName = "订单编号";
            dtData.Columns["OF_NAME"].ColumnName = "订单来源";
            dtData.Columns["ORDER_ID"].ColumnName = "关联订单";
            dtData.Columns["RD_CUSTOMERCODE"].ColumnName = "会员账号";

            dtData.Columns["RD_TRANCOST"].ColumnName = "物流费用";
            dtData.Columns["RD_TRANID"].ColumnName = "物流单号";
            dtData.Columns["RD_RETURNER"].ColumnName = "退货人";
            dtData.Columns["RD_RETURNPHONE1"].ColumnName = "电话";
            dtData.Columns["RD_RETURNERPHONE2"].ColumnName = "手机";
            dtData.Columns["RD_RETURNADDRESS"].ColumnName = "地址";
            dtData.Columns["RD_RETURNPOSTCODE"].ColumnName = "邮编";


            dtData.Columns["RD_QTY"].ColumnName = "商品数量";
            dtData.Columns["RD_AMT"].ColumnName = "商品金额";
            dtData.Columns["RD_OFFERAMT"].ColumnName = "让利金额";
            dtData.Columns["RD_AAMT"].ColumnName = "实际金额";


            dtData.Columns["RD_CREATER"].ColumnName = "制单人";
            dtData.Columns["RD_CREATEDATE"].ColumnName = "制单时间";
            dtData.Columns["RD_CHECKER"].ColumnName = "审核人";
            dtData.Columns["RD_CHECKDATE"].ColumnName = "审核时间";
            dtData.Columns["RD_ACHECKER"].ColumnName = "财务审核人";
            dtData.Columns["RD_ORDER_ACHECKDATE"].ColumnName = "财务审核时间";
            dtData.Columns["RD_DELETER"].ColumnName = "作废人";
            dtData.Columns["RD_DELETEDATE"].ColumnName = "作废时间";

            dtData.Columns["RD_CLEAREDAMT"].ColumnName = "已结算金额";
            dtData.Columns["RD_SUBITG"].ColumnName = "退还积分";
            dtData.Columns["RD_REASON"].ColumnName = "退货原因";
            dtData.Columns["RD_DUTY"].ColumnName = "责任人";
            dtData.Columns["RD_NOTE"].ColumnName = "备注";



            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                dtData.Rows[i]["含担保交易"] = dtData.Rows[i]["RD_SECDEAL"].ToString() == "1" ? "是" : "否";
                dtData.Rows[i]["审核"] = dtData.Rows[i]["RD_CHECK"].ToString() == "1" ? "已客审" : "未客审";
                dtData.Rows[i]["财务审核"] = dtData.Rows[i]["RD_ACHECK"].ToString() == "1" ? "已审核" : "未审核";
                dtData.Rows[i]["结算"] = dtData.Rows[i]["RD_CLEAR"].ToString() == "1" ? "已结算" : "未结算";
                dtData.Rows[i]["作废"] = dtData.Rows[i]["RD_DELETE"].ToString() == "1" ? "已作废" : "未作废";
            }

            ////移除列 
            dtData.Columns.Remove(dtData.Columns["RD_SECDEAL"]);
            dtData.Columns.Remove(dtData.Columns["RD_CHECK"]);
            dtData.Columns.Remove(dtData.Columns["RD_ACHECK"]);
            dtData.Columns.Remove(dtData.Columns["RD_CLEAR"]);
            dtData.Columns.Remove(dtData.Columns["RD_DELETE"]);

            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();

            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
    }
}
